package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.api.entity.Member;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.weixin.util.MD5Util;
import com.oceansoft.mobile.weixin.util.WeixinUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by liujie on 2016/3/28.
 * 交巡警用户对接跳转预处理
 */
@Controller
@RequestMapping("api/traffic")
public class ApiTrafficController {
    //交巡警页面主机
    static final String TrafficHost = "http://police.luopan88.com:8099/";
    //交巡警接口url
    static final String TrafficApi = "http://police.luopan88.com:8090/wechatSz/select.jsp";
    //静态变量
    static final String UserIsExist = "搜索成功";//用户存在
    static final String UserRegSuss = "注册成功";//注册成功
    static final String PasswordKey = "SZLP20160328";//MD5加密密钥
    static final String ApiAuth = "SZOS_20160318_2QW23ECDR87328KM0TQ";//调用授权

    @RequestMapping(value = "gotoTrafficByUser/{pageName}")
    public String gotoPage(@PathVariable("pageName") String pageName, HttpSession sessiion, HttpServletRequest request,
                           Model model) throws IllegalArgumentException, IOException {
        boolean b = WeixinUtil.checkIsLoginUser(request);
        if (!b) {
            model.addAttribute("openId", sessiion.getAttribute("openId"));
            model.addAttribute("gotoPage", "/api/netApprove/gotoPageWithUser/" + pageName);
            return "weixin/login/login";
        } else {
            String openId = (String) sessiion.getAttribute("openId");
            Member member = (Member) sessiion.getAttribute(Constant.SESSION_USER);
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("token", "n");
            jsonParam.put("who", ApiAuth);
            jsonParam.put("api", "userSearch");
            jsonParam.put("ver", "1");

//            jsonParam.put("yhdh",member.getLoginId());
            jsonParam.put("OpenId", member.getOpenId());
//            jsonParam.put("mm","");
            //查询用户是否存在
            String a = HttpClientUtil.doGet(TrafficApi, "a", System.currentTimeMillis(), "q", URLEncoder.encode(jsonParam.toJSONString(), "utf-8"));
            System.out.println(a);
            //JSONObject json = JSONObject.parseObject(a);
            JSONObject json = new JSONObject();
            json.put("res", "1");

            if ("1".equals((String) json.get("res")) && UserIsExist.equals((String) json.get("msg"))) {
                return "redirect:" + TrafficHost + pageName + ".html?openId=" + openId;
            } else {
                jsonParam = new JSONObject();
                jsonParam.put("token", "n");
                jsonParam.put("who", ApiAuth);
                jsonParam.put("api", "userRegister");
                jsonParam.put("ver", "1");

                jsonParam.put("yhdh", member.getLoginId());//用户代号
//                jsonParam.put("mm",MD5Util.getMD5String(PasswordKey+member.getPassword()));//密文密码
                jsonParam.put("mm", member.getPassword());//明文密码
                jsonParam.put("yhlx", "1");//用户类型(1-个人 2-其他)
                jsonParam.put("yhly", "0");//用户来源 (0-公安 1-交管)
                jsonParam.put("yhzc", "1");//用户注册 (1驾驶人注册 2机动车注册 3新学员注册)
                jsonParam.put("sjhm", member.getMobile());//手机号码
                jsonParam.put("sfzmmc", "A");//身份证明名称
                jsonParam.put("sfzmhm", member.getIdCard());//身份证明号码
                jsonParam.put("xm", member.getRealName());//姓名
                //TODO 无法获取微信号
                jsonParam.put("wxhm", member.getOpenId());//微信号
                jsonParam.put("wxid", member.getOpenId());//微信openid
                jsonParam.put("dzyx", member.getEmail());//电子邮箱
                jsonParam.put("lxzsxxdz", member.getAddress());//联系住所详细地址
                jsonParam.put("tzfsfw", "AB");//通知方式范围 (A电子邮箱、B手机短信 C app D网页E微信（允许多选）)
                jsonParam.put("zclx", "4");//注册类型 (1：互联网 2：面签 3：手机app 4：微信 9：其他)

                a = HttpClientUtil.doGet(TrafficApi, "a", System.currentTimeMillis(), "q", URLEncoder.encode(jsonParam.toJSONString(), "utf-8"));//注册用户
                System.out.println(a);
                json = JSONObject.parseObject(a);
                if ("1".equals((String) json.get("res")) && UserRegSuss.equals((String) json.get("msg"))) {
                    return "redirect:" + TrafficHost + pageName + ".html?openId=" + openId;
                }
            }
            return "redirect:" + TrafficHost + pageName + ".html?openId=" + openId;
        }
    }
}
